function AddVehicleView() {
	var self = Ti.UI.createView({
		top : 0,
	});

	// Data for the rows in our form
	var data = [];

	var headerRow = Ti.UI.createTableViewRow({
		height : 95,
		selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE
	});

	vehicleImage = Titanium.UI.createImageView({
		width : 80,
		height : 80,
		right : 20,
		hires : true,
		top : 10,
		image : 'car.png'
	});

	vehicleImage.addEventListener('click', function(e) {
		self.fireEvent('wantToTakeVehiclePhotoEvent');
	});

	var photoLabel = Ti.UI.createLabel({
		text : 'photo',
		font : {
			fontSize : 16,
			fontWeight : 'bold',
			fontFamily : 'Arial'
		},
		height : 'auto',
		width : 'auto',
		left : 10
	});
	
	headerRow.add(photoLabel);
	headerRow.add(vehicleImage);

	data.push(headerRow);

	// Car make
	var makeRow = Ti.UI.createTableViewRow({
		height : 50
	});
	var makeLabel = Ti.UI.createLabel({
		text : 'make',
		font : {
			fontSize : 16,
			fontWeight : 'bold',
			fontFamily : 'Arial'
		},
		height : 'auto',
		width : 'auto',
		left : 10
	});
	var makeField = Titanium.UI.createTextField({
		color : '#336699',
		height : 35,
		right : 20,
		width : 160,
		clearOnEdit : true,
		textAlign : 'right',
		font : {
			fontSize : 14,
			fontFamily : 'Arial'
		},
		keyboardType : Titanium.UI.KEYBOARD_DEFAULT,
		returnKeyType : Titanium.UI.RETURNKEY_DEFAULT
	});
	makeRow.add(makeLabel);
	makeRow.add(makeField);
	makeRow.addEventListener('click', function(e) {
		makeField.focus();
	});
	data.push(makeRow);

	// Car model
	var modelRow = Ti.UI.createTableViewRow({
		height : 50
	});
	var modelLabel = Ti.UI.createLabel({
		text : 'model',
		font : {
			fontSize : 16,
			fontWeight : 'bold',
			fontFamily : 'Arial'
		},
		height : 'auto',
		width : 'auto',
		left : 10
	});
	var modelField = Titanium.UI.createTextField({
		color : '#336699',
		height : 35,
		right : 20,
		width : 160,
		clearOnEdit : true,
		textAlign : 'right',
		font : {
			fontSize : 14,
			fontFamily : 'Arial'
		},
		keyboardType : Titanium.UI.KEYBOARD_DEFAULT,
		returnKeyType : Titanium.UI.RETURNKEY_DEFAULT
	});
	modelRow.add(modelLabel);
	modelRow.add(modelField);
	modelRow.addEventListener('click', function(e) {
		modelField.focus();
	});
	data.push(modelRow);

	// Car fuel type
	var fuelTypeRow = Ti.UI.createTableViewRow({
		height : 50
	});

	var fuelTypeLabel = Ti.UI.createLabel({
		text : 'fuel type',
		font : {
			fontSize : 16,
			fontWeight : 'bold',
			fontFamily : 'Arial'
		},
		height : 'auto',
		width : 'auto',
		left : 10
	});

	var fuelTypeValue = Ti.UI.createLabel({
		color : '#336699',
		font : {
			fontSize : 14,
			fontFamily : 'Arial'
		},
		right : 50,
		width : 80
	});

	var fuelTypeOptionsButton = Ti.UI.createButton({
		style : Titanium.UI.iPhone.SystemButton.DISCLOSURE,
		right : 15
	});

	// The following should be retrieved from database TODO
	var fuelTypes = ['Diesel', 'Gasoline', 'Ethanol (E85)', 'Cancel'];
	var fuelTypeDialog = Titanium.UI.createOptionDialog({
		title : 'Fuel Type',
		options : fuelTypes,
		cancel : 3
	});
	fuelTypeDialog.addEventListener('click', function(fuelTypeEvent) {
		if(fuelTypeEvent.index != fuelTypeEvent.cancel) {
			fuelTypeValue.show();
			fuelTypeValue.setText(fuelTypes[fuelTypeEvent.index]);
		}
	});
	var fuelTypeEventHandler = function(e) {
		fuelTypeDialog.show();
	}
	fuelTypeRow.addEventListener('click', fuelTypeEventHandler);
	fuelTypeRow.add(fuelTypeLabel);
	fuelTypeRow.add(fuelTypeValue);
	fuelTypeRow.add(fuelTypeOptionsButton);
	data.push(fuelTypeRow);

	// Car notes
	var notesRow = Ti.UI.createTableViewRow({
		height : 110
	});
	var notesLabel = Ti.UI.createLabel({
		text : 'notes',
		font : {
			fontSize : 16,
			fontWeight : 'bold',
			fontFamily : 'Arial'
		},
		height : 'auto',
		width : 'auto',
		left : 10
	});
	var notesField = Titanium.UI.createTextArea({
		color : '#336699',
		height : 90,
		right : 20,
		width : 180,
		top : 10,
		font : {
			fontSize : 14,
			fontFamily : 'Arial'
		},
		keyboardType : Titanium.UI.KEYBOARD_DEFAULT,
		suppressReturn : true,
		borderWidth : 2,
		borderColor : '#ddd',
		borderRadius : 5
	});
	notesRow.add(notesLabel);
	notesRow.add(notesField);
	notesLabel.addEventListener('click', function(e) {
		notesField.blur();
	});
	data.push(notesRow);

	// Create table view
	var tableView = Ti.UI.createTableView({
		data : data,
		style : Titanium.UI.iPhone.TableViewStyle.GROUPED,
		top : 0
	});

	self.add(tableView);

	// We set the form with some default values
	resetAll(makeField, modelField, fuelTypeValue, notesField, vehicleImage);

	// Event listeners
	self.addEventListener('gotVehiclePhotoEvent', function(e) {

		if(vehicleImage != null) {
			headerRow.remove(vehicleImage);
			vehicleImage = null;
		}

		vehicleImage = Titanium.UI.createImageView({
			width : 80,
			height : 80,
			right : 20,
			hires : true,
			top : 10
		});

		vehicleImage.setImage(e.thumbnailData);
		vehicleImage.setWidth(e.width);
		vehicleImage.setHeight(e.height);

		vehicleImage.addEventListener('click', function(e) {
			self.fireEvent('wantToTakeVehiclePhotoEvent');
		});

		imageBlob = e.thumbnailData;

		headerRow.add(vehicleImage);

		if(e.width > e.height) {
			isWidePhoto = 1;
		}
	});

	self.addEventListener('wantToSaveVehicleEvent', function(e) {

		// First we validate the form
		var validationError = validateForm(makeField.getValue(), modelField.getValue(), fuelTypeValue.getText());
		if(validationError) {
			Ti.UI.createAlertDialog({
				message : validationError,
				ok : 'Ok'
			}).show();
		} else {
			// Create object containing all necessary data
			var newVehicle = {
				make : makeField.getValue(),
				model : modelField.getValue(),
				fuelType : fuelTypeValue.getText(),
				notes : notesField.getValue(),
				photo : imageBlob,
				isWidePhoto : isWidePhoto
			};

			self.fireEvent('saveVehicleEvent', newVehicle);
		}
	});

	self.addEventListener('resetEvent', function(e) {
		resetAll(makeField, modelField, fuelTypeValue, notesField, vehicleImage);
		
		if(vehicleImage != null) {
			headerRow.remove(vehicleImage);
			vehicleImage = null;
		}

		vehicleImage = Titanium.UI.createImageView({
			width : 80,
			height : 80,
			right : 20,
			hires : true,
			top : 10,
			image:'car.png'
		});

		vehicleImage.addEventListener('click', function(e) {
			self.fireEvent('wantToTakeVehiclePhotoEvent');
		});
		
		headerRow.add(vehicleImage);
	});

	return self;
}

var exampleMake = 'e.g. BMW';
var exampleModel = 'e.g. 530d';
var imageBlob = null;
var isWidePhoto = 0;
var vehicleImage = null;

function resetAll(makeField, modelField, fuelTypeValue, notesField, vehicleImage) {
	makeField.setValue(exampleMake);
	modelField.setValue(exampleModel);
	fuelTypeValue.setText('');
	notesField.setValue('');

	// Also set imageBlob to null
	imageBlob = null;
}

function validateForm(make, model, fueltype) {
	var errorMessage;

	if(!make || make.length < 2 || make == exampleMake) {
		errorMessage = 'Need a valid car manufacturer name';
	} else if(!model || model.length < 2 || model == exampleModel) {
		errorMessage = 'Car model name is too short';
	} else if(!fueltype) {
		errorMessage = 'Fuel type must be selected';
	}
	return errorMessage;
}

module.exports = AddVehicleView;
